package chu.dejian.dialogOrMenu;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.sam.blemodule.R;
import com.sam.utils.T;
import com.sam.widget.calendar.CalendarView;
import com.sam.widget.button.StateBitmapImageButton;

public class CalendarDialog extends Dialog {
	private Context context;
	private CalendarView calendar;
	private StateBitmapImageButton calendarLeft;
	private TextView calendarCenter;
	private StateBitmapImageButton calendarRight;
	private SimpleDateFormat format;

	public CalendarDialog(Context context) {
		super(context,R.style.trans_bg_dialog_stye);
		this.context = context;
		setContentView(R.layout.dialog_calendar_dialog);
		init();
	}


	private void init() {
		format = new SimpleDateFormat("yyyy-MM-dd");
		//获取日历控件对象
		calendar = (CalendarView)findViewById(R.id.calendar);
		calendar.setSelectMore(false); //单选  
		
		calendarLeft = (StateBitmapImageButton)findViewById(R.id.calendarLeft);
		calendarCenter = (TextView)findViewById(R.id.calendarCenter);
		calendarRight = (StateBitmapImageButton)findViewById(R.id.calendarRight);
		try {
			//设置日历日期
			Date date = format.parse("2015-01-01");
			calendar.setCalendarData(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		//获取日历中年月 ya[0]为年，ya[1]为月（格式大家可以自行在日历控件中改）
		String[] ya = calendar.getYearAndmonth().split("-"); 
		calendarCenter.setText(ya[0] + "年" + ya[1] + "月");
		calendarLeft.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//点击上一月 同样返回年月 
				String leftYearAndmonth = calendar.clickLeftMonth(); 
				String[] ya = leftYearAndmonth.split("-"); 
				calendarCenter.setText(ya[0]+"年"+ya[1]+"月");
			}
		});
		
		calendarRight.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//点击下一月
				String rightYearAndmonth = calendar.clickRightMonth();
				String[] ya = rightYearAndmonth.split("-"); 
				calendarCenter.setText(ya[0]+"年"+ya[1]+"月");
			}
		});
		
		//设置控件监听，可以监听到点击的每一天（大家也可以在控件中根据需求设定）
		calendar.setOnItemClickListener(new CalendarView.OnItemClickListener() {
			
			@Override
			public void OnItemClick(Date selectedStartDate,
					Date selectedEndDate, Date downDate) {
				if(calendar.isSelectMore()){
					T.showShort(context, format.format(selectedStartDate) + "到" + format.format(selectedEndDate));
				}else{
					T.showShort(context, format.format(downDate));
				}
			}
		});
		
	}
}
